﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGunner1 : EnemyBaseFixed
{
    //[Tooltip("子弹偏移")]
    public Vector2 aimOffset = new(24f, 5f);
    private Vector2 aimAt = Vector2.right;

    private void Awake()
    {
        base.Init();
        boomPrefab=Constant.Prefabs.EnemyBoom2.Load();
    }
    private void Start()
    {
        Stop();
    }
    private void FixedUpdate()
    {
        if (this.running)
        {
            base.CommonFixedUpdate();
            UpdateFire();
        }
    }

    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = EnumState.Working;
    }
    protected void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (Utility.GetNearestPlayerPosition(transform.position, out Vector2 nearestPlayerPosition,Person.BodyPart.Head))
        {
            Vector2 direction = new Vector3(nearestPlayerPosition.x, nearestPlayerPosition.y, 0) - transform.position;
            aimAt = direction;
            aimAt.Normalize();
            direction.Normalize();
            lookAt = new Vector2(direction.x < 0 ? -1f : 1f, Mathf.Floor(direction.y + 0.5f));
            lookAt.x = lookAt.x == 0 ? 1 : lookAt.x;
        }

        animator.SetFloat("LookAtUp", lookAt.y);
    }

    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState)
    {
        base.UpdateStateDie(stateChanged, inState, out outState);
        dieAction.Update((SeqAction seq) =>
        {
            Vector3 diePosition = transform.position;
            if (!seq.PropertyVector.TryGetValue("diePosition", out diePosition))
            {
                diePosition = transform.position;
                seq.PropertyVector.Add("diePosition", diePosition);
            }
            int i = seq.seqNo;
            if (i < dieMovePixel.Length)
            {
                Vector2 pixel = dieMovePixel[i];
                float x = pixel.x / 16;
                float y = pixel.y / 16;
                transform.position = diePosition + new Vector3(-lookAt.x * x, y, 0);
            }
            else
            {
                if (boomPrefab != null)
                {
                    Instantiate(boomPrefab, transform.position, Quaternion.identity);
                }
                Constant.Sounds.EnemyDeath1.PlayOneShot();
                Destroy(gameObject);
            }
        });
    }

    private void UpdateFire()
    {
        fireAction.Update((SeqAction seq) =>
        {
            if (seq.IsFirstAction())
            {
                seq.PropertyInteger.Add("boxMagazine", boxMagazineSize);
            }
            Fire();
            int boxMagazine = seq.PropertyInteger["boxMagazine"];
            if (--boxMagazine <= 0)
            {
                seq.ReStart();
            }
            else
            {
                seq.PropertyInteger["boxMagazine"] = boxMagazine;
            }
        });
    }
    private void Fire()
    {
        Vector3 position = transform.position;
        Vector3 side = Vector3.zero;
        if (lookAt.y > 0)
        {
            side += (10f/16f)*Vector3.right + (38f/16f)*Vector3.up;
        }
        else if (lookAt.y == 0)
        {
            side += (15f / 16f) * Vector3.right + (26f / 16f) * Vector3.up;
        }
        else
        {
            side +=(15f / 16f) * Vector3.right + (14f / 16f) * Vector3.up;
        }
        if (lookAt.x < 0)
        {
            side.x *= -1;
        }
        position += side;

        
        GameObject objBullet = Instantiate(bulletPrefab, position, Quaternion.identity);
        if (objBullet.TryGetComponent<ABullet>(out var aBullet))
        {
            aBullet.SetBulletType(ABullet.BulletType.C);
            aBullet.SetSource(gameObject);
            aBullet.Launch(aimAt);
        }
    }

    public override void Run()
    {
        if (!running)
        {
            GameObject player = GameObject.FindWithTag("Player");
            float dirX;
            if (player != null)
            {
                dirX = player.transform.position.x - transform.position.x;
            }
            else
            {
                dirX = Camera.main.transform.position.x - transform.position.x;
            }
            lookAt = dirX >= 0 ? Vector2.right : Vector2.left;

            running = true;
        }
    }
}
